﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 人员去向
    /// </summary>
    public class Leave
    {
        		/// <summary>
		/// 增加一条数据
		/// </summary>
		public decimal Add(ElingOA6_Admin_DSJCJ.Model.Leave model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into dbo.t_Leave(");
            strSql.Append("FEntityNo,FEmplNo,FLeaveReasonCd,FDeptNo,FBeginDate,FEndDate,FReason,FInsEmpNo,FInsDate,FInsIP,FIsAdditonIns)");
			strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FEmplNo,@FLeaveReasonCd,@FDeptNo,@FBeginDate,@FEndDate,@FReason,@FInsEmpNo,@FInsDate,@FInsIP,@FIsAdditonIns)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FLeaveReasonCd", SqlDbType.NVarChar,6),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FReason", SqlDbType.NVarChar,500),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
                    new SqlParameter("@FIsAdditonIns", SqlDbType.Char,1)                    
                                        };
			parameters[0].Value = model.FEntityNo;
			parameters[1].Value = model.FEmplNo;
			parameters[2].Value = model.FLeaveReasonCd;
			parameters[3].Value = model.FDeptNo;
			parameters[4].Value = model.FBeginDate;
			parameters[5].Value = model.FEndDate;
			parameters[6].Value = model.FReason;
			parameters[7].Value = model.FInsEmpNo;
			parameters[8].Value = model.FInsDate;
			parameters[9].Value = model.FInsIP;
            parameters[10].Value = model.FIsAdditonIns;
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToDecimal(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.Leave model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  dbo.t_Leave set ");
            strSql.Append("FBeginDate=@FBeginDate,");
            strSql.Append("FEndDate=@FEndDate,");
            strSql.Append("FReason=@FReason,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdEmpNo=@FUpdEmpNo,");
            strSql.Append("FLeaveReasonCd=@FLeaveReasonCd,");
            strSql.Append("FEmplNo=@FEmplNo,");
            strSql.Append("FDeptNo=@FDeptNo,");
            strSql.Append("FUpdIP=@FUpdIP");
            strSql.Append(" where FLeaveID=@FLeaveID");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FReason", SqlDbType.NVarChar,500),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FLeaveID", SqlDbType.Decimal,13),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FLeaveReasonCd", SqlDbType.NVarChar,6),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FBeginDate;
            parameters[1].Value = model.FEndDate;
            parameters[2].Value = model.FReason;
            parameters[3].Value = model.FUpdDate;
            parameters[4].Value = model.FUpdIP;
            parameters[5].Value = model.FLeaveID;
            parameters[6].Value = model.FEmplNo;
            parameters[7].Value = model.FLeaveReasonCd;
            parameters[8].Value = model.FDeptNo;
            parameters[9].Value = model.FUpdEmpNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool LeaveOut(ElingOA6_Admin_DSJCJ.Model.Leave model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_Leave set ");
            strSql.Append("FSignOutDate=@FSignOutDate,");
            strSql.Append("FSignOutEmpNo=@FSignOutEmpNo,");
            strSql.Append("FSignOutIP=@FSignOutIP,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdEmpNo=@FUpdEmpNo,");
            strSql.Append("FUpdIP=@FUpdIP");
            strSql.Append(" where FLeaveID=@FLeaveID");
            SqlParameter[] parameters = {
					new SqlParameter("@FSignOutDate", SqlDbType.DateTime),
					new SqlParameter("@FSignOutIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FLeaveID", SqlDbType.Decimal,13),
					new SqlParameter("@FSignOutEmpNo", SqlDbType.NVarChar,15),
                    new SqlParameter("@FUpdDate", SqlDbType.DateTime),
                    new SqlParameter("@FUpdEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15)               
                                        };
            parameters[0].Value = model.FSignOutDate;
            parameters[1].Value = model.FSignOutIP;
            parameters[2].Value = model.FLeaveID;
            parameters[3].Value = model.FSignOutEmpNo;
            parameters[4].Value = model.FUpdDate;
            parameters[5].Value = model.FUpdEmpNo;
            parameters[6].Value = model.FUpdIP;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetLeaveStatisticsList(string begindate, string enddate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT [FLeaveID]
                                  ,[FEntityNo]
                                  ,[FEmplNo]
                                  ,[FLeaveReasonCd]
                                  ,[FLeaveReasonName]
                                  ,[FDeptNo]
                                  ,[FBeginDate]
                                  ,[FEndDate]
                                  ,[FReason]
                                  ,FClass
                                  FROM [ElingOA6_Admin_DSJCJ].[dbo].[v_LeaveStatistics] WHERE
                                  DATEDIFF(DAY,FBeginDate,@begindate)<=0 AND  DATEDIFF(DAY,FBeginDate,@enddate)>=0
                                  and FLeaveReasonCd IN (SELECT FLeaveReasonCd FROM dbo.t_cdLeaveReason WHERE FIsStat='Y')");
            SqlParameter[] parameters = {
					new SqlParameter("@begindate", SqlDbType.DateTime),
					new SqlParameter("@enddate", SqlDbType.DateTime)};
            parameters[0].Value = begindate;
            parameters[1].Value = enddate;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.Leave GetModel(decimal FLeaveID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FLeaveID,FEntityNo,FEmplNo,FLeaveReasonCd,FDeptNo,FBeginDate,FEndDate,FSignOutDate,FSignOutEmpNo,FSignOutIP,FReason,FInsEmpNo,FInsDate,FInsIP,FUpdEmpNo,FUpdDate,FUpdIP,FEmpName,FLeaveReasonName,FIsAdditonIns,FAuditEmplNo,FAuditDate,FAuditIP,FAuditNote from v_Leave ");
            strSql.Append(" where FLeaveID=@FLeaveID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLeaveID", SqlDbType.Decimal)
			};
            parameters[0].Value = FLeaveID;

            ElingOA6_Admin_DSJCJ.Model.Leave model = new ElingOA6_Admin_DSJCJ.Model.Leave();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.Leave DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.Leave model = new ElingOA6_Admin_DSJCJ.Model.Leave();
            if (row != null)
            {
                if (row["FLeaveID"] != null && row["FLeaveID"].ToString() != "")
                {
                    model.FLeaveID = decimal.Parse(row["FLeaveID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FLeaveReasonCd"] != null)
                {
                    model.FLeaveReasonCd = row["FLeaveReasonCd"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FBeginDate"] != null && row["FBeginDate"].ToString() != "")
                {
                    model.FBeginDate = DateTime.Parse(row["FBeginDate"].ToString());
                }
                if (row["FEndDate"] != null && row["FEndDate"].ToString() != "")
                {
                    model.FEndDate = DateTime.Parse(row["FEndDate"].ToString());
                }
                if (row["FSignOutDate"] != null && row["FSignOutDate"].ToString() != "")
                {
                    model.FSignOutDate = DateTime.Parse(row["FSignOutDate"].ToString());
                }
                if (row["FSignOutEmpNo"] != null)
                {
                    model.FSignOutEmpNo = row["FSignOutEmpNo"].ToString();
                }
                if (row["FSignOutIP"] != null)
                {
                    model.FSignOutIP = row["FSignOutIP"].ToString();
                }
                if (row["FReason"] != null)
                {
                    model.FReason = row["FReason"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmpNo"] != null)
                {
                    model.FUpdEmpNo = row["FUpdEmpNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row.Table.Columns.Contains("FEmpName"))
                {
                    if (row["FEmpName"] != null)
                    {
                        model.FEmplName = row["FEmpName"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FClass"))
                {
                    if (row["FClass"] != null)
                    {
                        model.FClass = row["FClass"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FLeaveReasonName"))
                {
                    if (row["FLeaveReasonName"] != null)
                    {
                        model.FLeaveReasonName = row["FLeaveReasonName"].ToString();
                    }
                }

                if (row.Table.Columns.Contains("FIsSignOut"))
                {
                    if (row["FIsSignOut"] != null)
                    {
                        model.FIsSignOut = row["FIsSignOut"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FIsAdditonIns"))
                {
                    if (row["FIsAdditonIns"] != null)
                    {
                        model.FIsAdditonIns = row["FIsAdditonIns"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FAuditEmplNo"))
                {
                    if (row["FAuditEmplNo"] != null)
                    {
                        model.FAuditEmplNo = row["FAuditEmplNo"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FAuditDate"))
                {
                    if (row["FAuditDate"] != null && row["FAuditDate"].ToString() != "")
                    {
                        model.FAuditDate = DateTime.Parse(row["FAuditDate"].ToString());
                    }
                }
                if (row.Table.Columns.Contains("FAuditIP"))
                {
                    if (row["FAuditIP"] != null)
                    {
                        model.FAuditIP = row["FAuditIP"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FAuditNote"))
                {
                    if (row["FAuditNote"] != null)
                    {
                        model.FAuditNote = row["FAuditNote"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.Leave VDataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.Leave model = new ElingOA6_Admin_DSJCJ.Model.Leave();
            if (row != null)
            {
                if (row["FLeaveID"] != null && row["FLeaveID"].ToString() != "")
                {
                    model.FLeaveID = decimal.Parse(row["FLeaveID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FLeaveReasonCd"] != null)
                {
                    model.FLeaveReasonCd = row["FLeaveReasonCd"].ToString();
                }
                if (row["FLeaveReasonName"] != null)
                {
                    model.FLeaveReasonName = row["FLeaveReasonName"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FBeginDate"] != null && row["FBeginDate"].ToString() != "")
                {
                    model.FBeginDate = DateTime.Parse(row["FBeginDate"].ToString());
                }
                if (row["FClass"] != null)
                {
                    model.FClass = row["FClass"].ToString();
                }
                if (row["FEndDate"] != null && row["FEndDate"].ToString() != "")
                {
                    model.FEndDate = DateTime.Parse(row["FEndDate"].ToString());
                }
                if (row["FReason"] != null)
                {
                    model.FReason = row["FReason"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FLeaveID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Leave ");
            strSql.Append(" where FLeaveID=@FLeaveID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLeaveID", SqlDbType.Decimal)
			};
            parameters[0].Value = FLeaveID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FLeaveIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Leave ");
            strSql.Append(" where FLeaveID in (" + FLeaveIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 需要参与去向管理的人员名单
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns></returns>
        public DataSet GetListByEntityNo(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.* from v_HRDeptEmpl a,dbo.t_HRDept b");
            strSql.Append(" WHERE a.FDeptNo=b.FDeptNo AND  a.FEntityNo=@FEntityNo and FIsMainPosition='Y'  ");
            strSql.Append("  and FIsJoin='Y' AND FEmpStatus <> '99' AND FIsAgency='N'");
            strSql.Append("  order by a.FDeptNo,a.FSeq,FDutyNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新
        /// </summary>
        public void UpdataEmpl(List<Model.HREmpl> emplModelList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            if (emplModelList.Count > 0)
            {
                strSql.Append(" update t_HREmpl set");
                strSql.Append(" FIsJoin=@FIsJoin");
                strSql.Append(" where FEntityNo=@FEntityNo and FEmplNo=@FEmplNo ");

                foreach (Model.HREmpl model in emplModelList)
                {

                    cmds = new CommandInfo();

                    SqlParameter[] parameters = {
                        new SqlParameter("@FIsJoin", SqlDbType.NVarChar,1),
					    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)};
                    parameters[0].Value = model.FIsJoin;
                    parameters[1].Value = model.FEntityNo;
                    parameters[2].Value = model.FEmplNo;
                    cmds.CommandText = strSql.ToString();
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);
                }
            }
            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }

        /// <summary>
        /// 审核外出登记
        /// <creator>sunlj</creator>
        /// <date>2014-05-22</date>
        /// </summary>
        /// <param name="LeaveModel"></param>
        public void AuditLeave(Model.Leave LeaveModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"UPDATE dbo.t_Leave SET
                             FAuditEmplNo=@FAuditEmplNo,
                             FAuditDate=GETDATE(),
                             FAuditIP=@FAuditIP,
                             FAuditNote=@FAuditNote,
                             FUpdEmpNo=@FUpdEmpNo,
                             FUpdDate=GETDATE(),
                             FUpdIP=@FUpdIP
                             WHERE FLeaveID=@FLeaveID
                            ");
            SqlParameter[] parameters = {
                        new SqlParameter("@FAuditEmplNo",SqlDbType.NVarChar,15),
                        new SqlParameter("@FAuditIP",SqlDbType.NVarChar,15),
                        new SqlParameter("@FAuditNote",SqlDbType.Char,1),
                        new SqlParameter("@FUpdEmpNo",SqlDbType.NVarChar,15),
                        new SqlParameter("@FUpdIP",SqlDbType.NVarChar,15),
                        new SqlParameter("@FLeaveID",SqlDbType.Decimal,20)
                                        };
            parameters[0].Value = LeaveModel.FAuditEmplNo;
            parameters[1].Value = LeaveModel.FAuditIP;
            parameters[2].Value = LeaveModel.FAuditNote;
            parameters[3].Value = LeaveModel.FUpdEmpNo;
            parameters[4].Value = LeaveModel.FUpdIP;
            parameters[5].Value = LeaveModel.FLeaveID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }





    }
}
